#!/usr/bin/python
#  This is part of the Scopia data visualization example collection
#
#
#  Copyright (C) 2008  Scopia Visual Interfaces Systems 
#                      http://www.scopia.es
#
#  author: Alejandro Conty
#  contributors:
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

import scopiavf.engine.glcontext as glcontext
import sys
import math
import time
from style.default import Style
from numpy import *
from scopiavf.engine.tools import *
from chart import *
from scopiavf.engine.rendertree import *

import nodes

ESCAPE = '\033'

class manager_t( glcontext.GlContext ):
    "GlTree application inheriting from GlContext"

    capture = False

    def __init__( self, w, h, chart):
        glcontext.GlContext.__init__(self, w, h )
        self.chart = chart
        self.timeMin = chart.transactions[0][V_TIME]
        self.timeMax = chart.transactions[-1][V_TIME]
        self.timeTotal = chart.transactions[-1][V_TIME] - chart.transactions[0][V_TIME]
        self.viewCenter = (0,0)
        self.viewRadius = 1
        self.timers = []
        self.style = Style( self )
        share = nodes.SharesMainNode( self, self.style, chart )
        self.root = RenderViewportNode( self, self.style, share, 0, 0, 1, 1 )


    def display(self):
        "Display function"
        self.depthTest( False )
        self.root.render()

    def keyboard(self, key, x, y):
        if key == 'c' or key == 'C':
            self.capture = not self.capture
        elif key == ESCAPE:
            print 'Stoping background works'
            print 'Done'
            sys.exit()
    
    def onSelect(self, path, node): pass

    def animateTime(self, elapsed):
        speed = self.timeTotal / 4.0
        self.common.timePos += speed * elapsed
        if self.common.timePos > self.timeMax: self.common.timePos = self.timeMin
        return True
